app.controller('BusCityListCtrl', function ($scope, ajax, $modal, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.reload = $scope.page.loader('city/bs/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);
	$scope.queryMap = {};
	$scope.query = function () { $scope.reload(1, $scope.queryMap); };

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/bus/city/writeModal.html?v=37507637ea',
			controller: 'BusCityModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'city/bs/update';
				$scope.page.mod(updater, $scope.reload);
			} else if (param.type == 'insert') {
				inserter.fields = param.data;
				inserter.resource = 'city/bs/insert';
				$scope.page.add(inserter, $scope.reload);

			}
		});
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'city/bs/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			}
		});
	};

	$scope.populars = function (size, submit_type) {
		$modal.open({
			templateUrl: 'template/bus/city/popular.html?v=4651256890',
			controller: 'BusPopularModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});
	};

});
